package com.qf.filter;

import com.qf.pojo.User;
import com.qf.service.UserService;
import com.qf.service.impl.UserServiceImpl;

import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.net.URLDecoder;

/**
 * 自动登录的过滤器
 */
@WebFilter(value = "/jsp/login.jsp",dispatcherTypes = DispatcherType.REQUEST) //默认直接访问这个地址进行过滤
public class AutoLoginFilter implements Filter {
    public void init(FilterConfig config) throws ServletException {
        System.out.println("AutoLoginFilter初始化了");
    }

    //销毁
    public void destroy() {
    }

    /**
     * 过滤任务
     * @param req 请求对象
     * @param resp  响应对象
     * @param chain 过滤链
     * @throws ServletException
     * @throws IOException
     */
    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {


        //1)req,resp---强转为HttpServletRequest/HttpServletResponse
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;

        //2)获取所有的cookie(当浏览器再次请求服务器的时候 "/jsp/login.jsp")
        Cookie[] cookies = request.getCookies();
        //声明cookie的内容
        String content = null ;

        if(cookies!=null){
            //浏览器中有cookie
            //遍历cookie
            for(Cookie cookie :cookies){
                //获取到每一个Cookie ,判断如果当前cookie的名称是"autouser",获取它的内容
                if("autouser".equals(cookie.getName())){
                    content = cookie.getValue() ;
                }
            }
            //判断如果当前获取到的content不为null,
            if(content!=null){
                //加密的内容----Jdk解码器URLDecoder--->public static String decode(String s, String enc)
                //参数1:字符串内容
                //参数2:字符集格式
                content = URLDecoder.decode(content, "utf-8");
                //"用户名=密码"---通过"="拆分内容
                String[] strs = content.split("=");
                System.out.println("过滤器中获取到用户信息--->"+strs[0]+"---"+strs[1]);
                //strs[0] :用户名
                //strs[1] :密码

                //调用UserService业务接口
                UserService userService = new UserServiceImpl() ;
                User user = userService.getUseByUserNameAndPwd(strs[0], strs[1]) ;

                //获取HttpSession
                HttpSession session = request.getSession();
                if(user!=null){
                    session.setAttribute("user" ,user) ;
                    //重定向到首页
                    response.sendRedirect(request.getContextPath()+"/jsp/index.jsp");
                }else{
                    //user没有找到
                    //放行
                    chain.doFilter(request,response);
                }
            }else{
                //放行
                chain.doFilter(request,response) ;
            }



        }else{
            //没有cookie
            //放行
            chain.doFilter(request,response);
        }


    }
}
